package com.adt.studioms.api;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.adt.studioms.om.PhotoSize;
import com.asao.baseware.core.api.hibernate.HibernateDAOAwareAPI;

public class PhotoSizeAPI extends HibernateDAOAwareAPI<PhotoSize> {

    protected Class<PhotoSize> getEntityClass() {
        return PhotoSize.class;
    }

    public PhotoSize createPhotoSize(String code, String name) {
        PhotoSize ps = new PhotoSize();
        ps.setCode(code);
        ps.setName(name);
        
        dao.save(ps);
        return ps;
    }
    
    public PhotoSize getByCode(String code) {
        DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
        criteria.add(Restrictions.eq("code", code));
        
        List<PhotoSize> photosizes = dao.findByCriteria(criteria, 0, 1);
        return photosizes.size()>0 ? photosizes.get(0) : null;
    }
}
